package com.ohs.bll.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	public static String BASE_DATE_FORMAT = "dd/MM/yyyy";
	
	public static String BASE_DATE_TIME_FORMAT = "dd/MM/yyyy HH:mm";
	
	public static String DATE_SEPARATOR_SLASH = "/";
	
	public static String DATE_SEPARATOR_DASH = "-";
	
	public static Date covertToStartOfDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	public static String getTime(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return formatTime(cal.get(Calendar.HOUR_OF_DAY)) + ":" + formatTime(cal.get(Calendar.MINUTE));
	}
	
	public static boolean time1BeforeTime2(String time1, String time2, int range) {
		String[] t1 = time1.split(":");
		String[] t2 = time2.split(":");
		int hour1 = Integer.parseInt(t1[0]);
		int min1 = Integer.parseInt(t1[1]);
		int hour2 = Integer.parseInt(t2[0]);
		int min2 = Integer.parseInt(t2[1]);
		int total1 = hour1*60 + min1;
		int total2 = hour2*60 + min2;
		if(total1 + range < total2) {
			return true;
		}
		return false;
	}
	
	public static Date stringToDate(String str) throws ParseException {
		DateFormat df = new SimpleDateFormat(BASE_DATE_FORMAT);
		Date day = df.parse(str);
		return day;
	}
	
	public static String dateToString(Date date) throws ParseException {
		DateFormat formatter = new SimpleDateFormat(BASE_DATE_TIME_FORMAT);
		return formatter.format(date);
	}
	
	public static String getDate(Date date) throws ParseException {
		DateFormat formatter = new SimpleDateFormat(BASE_DATE_FORMAT);
		return formatter.format(date);
	}
	
	public static Date stringToDate(String str, String time) throws ParseException {
		DateFormat df = new SimpleDateFormat(BASE_DATE_FORMAT + " HH:mm");
		Date day = df.parse(str + " " + time);
		return day;
	}
	
	public static String formatDateString(String date, String dateFormat, String outputDateSeparator) {
		String formatedDate = "";
		if (dateFormat.equals(BASE_DATE_FORMAT) && date != null && !"".equals(date)) {
			String[] strs = date.split("/");
			formatedDate = strs[2] + outputDateSeparator + strs[1] + outputDateSeparator + strs[0];
		}
		return formatedDate;
	}
	
	private static String formatTime(int number) {
		return (number < 10) ? "0" + number : number + "";	
	}
}
